package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.*;

public class ListaDinoTutti extends JFrame implements ActionListener,Serializable {
	
	
	
	private JButton[] dinosauri1;
	private JButton[] dinosauri2;
	
	private int risp ;
	private int[] lista1;
	private int[] lista2;
	
	private static final int  X = 200;
	private static final int  Y = 30;
	
	public ListaDinoTutti(int[] listamiei, int[] listanonmiei){
		
		super("Lista Dinosauri");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setLayout(new FlowLayout());
		
		lista1 = new int[listamiei.length];
		lista1 = listamiei;
		lista2 = new int[listanonmiei.length];
		lista2 = listanonmiei;
		
		int max;
		if(listamiei.length>=listanonmiei.length)
			max=listamiei.length;
		else
			max=listanonmiei.length;
		
		JPanel pannello1 = new JPanel();
		
		pannello1.setLayout(new GridLayout(max+1,1));
		JLabel miei = new JLabel("Miei dino");
		pannello1.add(miei);
		dinosauri1 = new JButton[listamiei.length];
		for(int i=0;i<max;i++){
			if(i>=listamiei.length){
				JLabel vuoto=new JLabel();
				vuoto.setPreferredSize(new Dimension(X, Y));
				pannello1.add(vuoto);
			}
			else{
			dinosauri1[i]=new JButton("dinosauro "+listamiei[i]);
			dinosauri1[i].setPreferredSize(new Dimension(X, Y));
			dinosauri1[i].addActionListener(this);
			pannello1.add(dinosauri1[i]);
			}
		}
		JPanel pannello2 = new JPanel();
		
		pannello2.setLayout(new GridLayout(max+1,1));
		JLabel nonmiei = new JLabel("Dino altrui");
		pannello2.add(nonmiei);
		dinosauri2 = new JButton[listanonmiei.length];
		for(int i=0;i<max;i++){
			if(i>=listanonmiei.length){
				JLabel vuoto=new JLabel();
				vuoto.setPreferredSize(new Dimension(X, Y));
				pannello2.add(vuoto);
			}
			else{
			dinosauri2[i]=new JButton("dinosauro "+listanonmiei[i]);
			dinosauri2[i].setPreferredSize(new Dimension(X, Y));
			dinosauri2[i].addActionListener(this);
			pannello2.add(dinosauri2[i]);
			}
		}
		
		add(pannello1);
		add(pannello2);	
		
		pack();
		validate();
		setVisible(true);	
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			for(int i=0;i<lista1.length;i++){
				if(e.getSource().equals(dinosauri1[i])){
				risp=lista1[i];
				notifyAll();
				}
			}
			for(int i=0;i<lista2.length;i++){
				if(e.getSource().equals(dinosauri2[i])){
				risp=lista2[i];
				notifyAll();		
				}
			}
		}
	}
	
	public int getRisp(){
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		dispose();
		return risp;
	}
	
}